
#include "cocos2d.h"
#include "SimpleAudioEngine.h"
#include "CCEGLView.h"

#include "AppDelegate.h"
#include "common/GB2ShapeCache-x.h"

#include "data/UserTestData.h"
#include "scene/Scene_Tutorial.h"

using namespace cocos2d;
using namespace CocosDenshion;


USING_NS_CC;


AppDelegate::AppDelegate()
{
}

AppDelegate::~AppDelegate()
{
	CUserData::releaseUserData();
	GB2ShapeCache::releaseGB2ShapeCache();
	Scene_Game::releaseSharedScene();
    SimpleAudioEngine::end();
}

bool AppDelegate::initInstance()
{
    bool bRet = false;

    do 
    {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
        // Initialize OpenGLView instance, that release by CCDirector when application terminate.
        // The HelloWorld is designed as HVGA.
        /*CCEGLView *pMainWnd = new CCEGLView();
        CC_BREAK_IF(!pMainWnd || !pMainWnd->Create(TEXT("Rolling Hero"), 1280, 720));*/
#endif  // CC_PLATFORM_WIN32

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
        // OpenGLView initialized in testsAppDelegate.mm on ios platform, nothing need to do here.
#endif  // CC_PLATFORM_IOS

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)

		// OpenGLView initialized in HelloWorld/android/jni/helloworld/main.cpp
		// the default setting is to create a fullscreen view
		// if you want to use auto-scale, please enable view->create(320,480) in main.cpp
		// if the resources under '/sdcard" or other writeable path, set it.
		// warning: the audio source should in assets/
		// cocos2d::CCFileUtils::setResourcePath("/sdcard");

#endif  // CC_PLATFORM_ANDROID

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WOPHONE)
        // Initialize OpenGLView instance, that release by CCDirector when application terminate.
        // The HelloWorld is designed as HVGA.
        CCEGLView *pMainWnd = new CCEGLView(this);
        CC_BREAK_IF(!pMainWnd || !pMainWnd->Create(320, 480, WM_WINDOW_ROTATE_MODE_CW));

#ifndef _TRANZDA_VM_  
		// on wophone emulator, we copy resources files to Work7/NEWPLUS/TDA_DATA/Data/ folder instead of zip file
        CCFileUtils::setResource("RollingHero.zip");
#endif
#endif  // CC_PLATFORM_WOPHONE
#if (CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE)
        // MaxAksenov said it's NOT a very elegant solution. I agree, haha
        CCDirector::sharedDirector()->setDeviceOrientation(kCCDeviceOrientationLandscapeLeft);
#endif
#if (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
        // Initialize OpenGLView instance, that release by CCDirector when application terminate.
        // The HelloWorld is designed as HVGA.
        CCEGLView *pMainWnd = new CCEGLView();
        CC_BREAK_IF(!pMainWnd || !pMainWnd->Create("cocos2d: Hello World", 800, 480, 480, 320));

        CCFileUtils::setResourcePath("../Resource/");
#endif  // CC_PLATFORM_LINUX
#if (CC_TARGET_PLATFORM == CC_PLATFORM_BADA)
        CCEGLView *pMainWnd = new CCEGLView();
        CC_BREAK_IF(!pMainWnd || !pMainWnd->Create(this, 480, 320));
        pMainWnd->setDeviceOrientation(Osp::Ui::ORIENTATION_LANDSCAPE);
        CCFileUtils::setResourcePath("/Res/");
#endif  // CC_PLATFORM_BADA
#if (CC_TARGET_PLATFORM == CC_PLATFORM_QNX)
        CCEGLView *pMainWnd = new CCEGLView();
        CC_BREAK_IF(!pMainWnd || !pMainWnd->Create(480, 320));
        CCFileUtils::setResourcePath("./app/native/Resource");
#endif // CC_PLATFORM_QNX

        bRet = true;
    } while (0);

    return bRet;
}

bool AppDelegate::applicationDidFinishLaunching()
{

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
	CCEGLView::sharedOpenGLView()->resize(1280,720);
	CCEGLView::sharedOpenGLView()->setFrameSize(1280,720);
#endif

	CCDirector *pDirector = CCDirector::sharedDirector();
	pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());

    CCSize screenSize = CCEGLView::sharedOpenGLView()->getFrameSize();

    CCSize designSize = CCSizeMake(1280, 720);
    
    /*if (screenSize.height > 320)
    {
        CCSize resourceSize = CCSizeMake(1280, 720);
        CCFileUtils::sharedFileUtils()->setResourceDirectory("hd");
        pDirector->setContentScaleFactor(resourceSize.height/designSize.height);
    }*/

    /*CCEGLView::sharedOpenGLView()->setDesignResolutionSize(designSize.width, designSize.height, kResolutionNoBorder);*/
	CCEGLView::sharedOpenGLView()->setDesignResolutionSize(designSize.width, designSize.height, kResolutionShowAll);
	
    // sets landscape mode
   // pDirector->setDeviceOrientation(kCCDeviceOrientationPortrait/*kCCDeviceOrientationLandscapeLeft*/);

	// turn on display FPS
	//pDirector->setDisplayStats(true);

	pDirector->setDepthTest(false);

	pDirector->setProjection( kCCDirectorProjection2D );

	// set FPS. the default value is 1.0/60 if you don't call this
    pDirector->setAnimationInterval(1.0 / 60);
	  
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
	 //CUserData::sharedUserData()->LoadUserData("");
#endif
	
	 Scene_Loading *pScene = Scene_Loading::create();
	 pDirector->runWithScene(pScene);

    return true;
}

// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground()
{
    CCDirector::sharedDirector()->pause();

	CUserData::sharedUserData()->SaveUserData();

#if 1
    CUserSetting::sharedUserSetting()->pause_bgm();
#else
    SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
    SimpleAudioEngine::sharedEngine()->pauseAllEffects();
#endif
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground()
{
    CCDirector::sharedDirector()->resume();

    Scene_Loading *sceneLoading = dynamic_cast<Scene_Loading *>(CCDirector::sharedDirector()->getRunningScene());
    if (sceneLoading != NULL)
    {
        sceneLoading->onResume();
    }
    else
    {
		Scene_Game *sceneGame = dynamic_cast<Scene_Game *>(CCDirector::sharedDirector()->getRunningScene());

		if(sceneGame)
		{
			Scene_Game::sharedScene()->onResume();
		}
		else
		{
			Scene_Tutorial *sceneTutorial = dynamic_cast<Scene_Tutorial *>(CCDirector::sharedDirector()->getRunningScene());

			if(sceneTutorial)
			{
				sceneTutorial->onResume();
			}
		}
    }

#if 1
    CUserSetting::sharedUserSetting()->resume_bgm();
#else
    SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
    SimpleAudioEngine::sharedEngine()->resumeAllEffects();
#endif
}

CUserData* AppDelegate::getUserData()
{
	return CUserData::sharedUserData();
}

Scene_Game* AppDelegate::getGameScene()
{
	return Scene_Game::sharedScene();
}